package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"sync"
)

func httpDo() {
	// msg
	//msg := []byte(`{"condition_code":"12-2-192.168.90.2","connector_id":2,"message_type":0,"tenant_id":12,"local_ip":"192.168.90.2","version":"1.0.0.1","run_time":36000,"cpu":8,"memory":12,"cserver_connect":1,"cvpn_connect_success":9,"cvpn_connect_exception":0,"domain_resolution_failed":0,"session_count":2327,"dt_create":1645177420000}`)
	msg := []byte(`{"name":"lbs","age":22}`)
	bodyBuf := bytes.NewBuffer(msg)

	// 构建http客户端
	client := &http.Client{}
	req, err := http.NewRequest("POST",
		"http://10.0.24.4:9191/saas/info/spa/v1/point/trigger",
		bodyBuf)
	if err != nil {
		fmt.Println("new request err : ", err)
		return
	}

	// 设置http头
	req.Header.Set("Content-Type", "application/json")

	// 发送http请求
	rsp, err := client.Do(req)
	if err != nil {
		fmt.Println("send req err : ", err)
		return
	}
	defer rsp.Body.Close()

	// 获取返回值
	body, err := ioutil.ReadAll(rsp.Body)
	if err != nil {
		fmt.Println("read rsp err : ", err)
		return
	}
	if rsp != nil {
		fmt.Println("rsp status : ", rsp.Status)
		fmt.Println("rsp code   : ", rsp.StatusCode)
		fmt.Println("rsp proto  : ", rsp.Proto)
		fmt.Println("rsp body   : ", string(body))
	}
}

func main() {
	wg := sync.WaitGroup{}
	for i := 0; i < 10; i++{
		go func(wg *sync.WaitGroup) {
			wg.Add(1)
			defer wg.Done()
			httpDo()
		}(&wg)
	}
	wg.Wait()
}